﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace Util.Setvices.Controllers
{

    public class EncryptRequest
    {
        public String content { get; set; } = "";
    }

    [Route("[controller]")]
    public class EncryptController : Controller
    {

        public readonly string aesKey = "shanghai_yaoning";
        public readonly string aesIv = "shanghaixiaobang";

        [HttpPost]
        [Route("aes/encode")]
        public string lg_aesEncode([FromBody] EncryptRequest request)
        {
            if (string.IsNullOrEmpty(request.content))
            {

            }
            var sourceBytes = System.Text.Encoding.UTF8.GetBytes(request.content);
            var aes = System.Security.Cryptography.Aes.Create();
            aes.Mode = System.Security.Cryptography.CipherMode.CBC;
            aes.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
            aes.Key = System.Text.Encoding.UTF8.GetBytes(aesKey);
            aes.IV = System.Text.Encoding.UTF8.GetBytes(aesIv);
            var transform = aes.CreateEncryptor();
            return System.Convert.ToBase64String(transform.TransformFinalBlock(sourceBytes, 0, sourceBytes.Length));
        }

        [HttpPost]
        [Route("aes/decode")]
        public string lg_aesDecode([FromBody] EncryptRequest request)
        {
            if (string.IsNullOrEmpty(request.content))
            {

            }
            var encryptBytes = System.Convert.FromBase64String(request.content);
            var aes = System.Security.Cryptography.Aes.Create();
            aes.Mode = System.Security.Cryptography.CipherMode.CBC;
            aes.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
            aes.Key = System.Text.Encoding.UTF8.GetBytes(aesKey);
            aes.IV = System.Text.Encoding.UTF8.GetBytes(aesIv);
            var transform = aes.CreateDecryptor();
            return System.Text.Encoding.UTF8.GetString(transform.TransformFinalBlock(encryptBytes, 0, encryptBytes.Length));
        }
    }
}
